package pruebas;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import junit.framework.TestCase;
import negModel.AlgoPesos;
import negModel.Escape;
import negModelExcep.*;

public class EscapeTest extends TestCase {

	Escape esc1,esc2;
	Element e;
	
	protected void setUp() throws Exception {
		super.setUp();
		esc1 = new Escape(100,20,70,new String("escape"),new AlgoPesos(100),true,100,8);

		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
	    DocumentBuilder loader = factory.newDocumentBuilder();
	    Document doc = loader.newDocument();
	    
	    e = doc.createElement("parte");
		e.setAttribute("Clase","escape");
		
		e.setAttribute("maxVU", ""+ esc1.getMaxVU());
		e.setAttribute("vidaUtil", ""+ esc1.getVidaUtil());
		e.setAttribute("peso", ""+ esc1.getPeso());
		e.setAttribute("aportevmax", ""+ esc1.aportevmax());
		e.setAttribute("detalle", esc1.getDetalle());
		e.setAttribute("precio", ""+(esc1.getPrecio()).getMonto());
		e.setAttribute("removible", ""+esc1.esRemovible());
		e.setAttribute("performance", ""+esc1.getPerformance());
		
		e.setAttribute("porcentajeElim", ""+esc1.getPorcentajeElim());
		
	}

	public void testGastar() {
		try {
			esc1.gastar();
		} catch (eParteNoFunciona e) {
			fail("se elevo la excepcion");
		}
		assertEquals(99,esc1.getVidaUtil(),0.1);
		
	}

	public void testFuncionar() {
		try {
			assertEquals(0.08,esc1.funcionar(),0.1);
		} catch (eParteNoFunciona e) {
			fail("se elevo la excepcion");
		}
		
	}
	
	public void testToXml () throws Exception {
		
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
	    DocumentBuilder loader = factory.newDocumentBuilder();
	    Document doc = loader.newDocument();
		
		e=esc1.toXml(doc);
		
		assertEquals(esc1.getPorcentajeElim(),Float.parseFloat(e.getAttribute("porcentajeElim")),0.0);
		
		assertEquals(esc1.getPerformance(),Integer.parseInt(e.getAttribute("performance")));
		assertEquals((esc1.getPrecio()).getMonto(),Double.parseDouble(e.getAttribute("precio")),0.0);
		assertEquals(esc1.aportevmax(),Float.parseFloat(e.getAttribute("aportevmax")),0.0);
		assertEquals(esc1.getDetalle(),e.getAttribute("detalle"));
		assertEquals(esc1.getPeso(),Double.parseDouble(e.getAttribute("peso")),0.0);
		assertEquals(esc1.getVidaUtil(),Integer.parseInt(e.getAttribute("vidaUtil")));
		
	}
	
	public void testEscape()throws Exception {
		
		esc2=new Escape(e);
				
		assertEquals(esc2.getPorcentajeElim(),Float.parseFloat(e.getAttribute("porcentajeElim")),0.0);
		
		assertEquals(esc2.getPerformance(),Integer.parseInt(e.getAttribute("performance")));
		assertEquals((esc2.getPrecio()).getMonto(),Double.parseDouble(e.getAttribute("precio")),0.0);
		assertEquals(esc2.aportevmax(),Float.parseFloat(e.getAttribute("aportevmax")),0.0);
		assertEquals(esc2.getDetalle(),e.getAttribute("detalle"));
		assertEquals(esc2.getPeso(),Double.parseDouble(e.getAttribute("peso")),0.0);
		assertEquals(esc2.getVidaUtil(),Integer.parseInt(e.getAttribute("vidaUtil")));
		
	}

}
